var vueApp = new Vue({
  el: '#app',
  data: function () {
    return {
    	title:'字典添加',
    	url:'dictlist.html',
    	dictId:'',
    	oper:'add',  
    	code:'',
    	code_desc:'',
    	display_order:'',
    	rmk:'',
    	dict_logo:'',
//    	err_logo:'../../plugin/images-app/error.jpg',
    	dft_logo:'../../plugin/images-app/no-img.jpg',  // 默认显示
    	err_vlogo:'this.src="../../plugin/images-app/error.jpg"',
    	dictData:[],
    	idx:0,
    	uploader:{},
    	upload:{
    		size: 5,
    		folder:'/file/msg',
    		mimeTypes:'image/jpg,image/jpeg,image/png',
    		exts:'jpg,jpeg,png',
    		modeName:'字典录入'
    	},
    	logo_idx:0
    };
  },
  mounted: function () {
//    userEx.getUserInfo(function(data){
//	    tools.checkPriv(data.userinfo.privNames,"dict_set");
//	});    
    this.init();
  },
  methods: {
    init: function () {      
    	this.dictId = tools.getQueryString("dictId");
	    this.oper = tools.getQueryString("oper")==""?this.oper:tools.getQueryString("oper");
	    this.setTitle();
	    if(this.oper=="edit"){
	    	this.getInfo();
	    }else{
	    	this.initUploader();
	    }
    	 
    },    
    setTitle:function(){
    	if(this.oper=="add"){
    		$("title").html("字典添加");
    		this.title = "字典添加";
    	}else if(this.oper=="edit"){
    		$("title").html("字典编辑");
    		this.title="字典编辑";
    	}
    },
    getInfo:function(){
    	var that =this;
//    	var data = {"mastlist":[{
//    	    "code":"rank",
//    	    "code_desc":"查看范围",
//    	    "detaillist":[
//    	        { "code":"1",
//    	          "code_desc":"本报社",
//    	          "dict_logo":"",
//    	          "display_order":"1",
//    	          "remarks":""
//    	        },
//    	        { "code":"2",
//    	          "code_desc":"草原云",
//    	          "dict_logo":"",
//    	          "display_order":"2",
//    	          "remarks":""
//    	        },
//    	        { "code":"rank_3",
//    	          "code_desc":"本部室",
//    	          "dict_logo":"../a.jpg",
//    	          "display_order":"3",
//    	          "remarks":""
//    	        }
//    	     ],
//    	        "dict_logo":"/file/images/upload/dict/0674b576181f45ceb4249183c1a5bc19.png",
//    	        "display_order":"1",
//    	        "remarks":""
//    	     }],
//    	        "respCode":"0",
//    	        "respDesc":"数据获取成功"
//    	  };
//    	 if(data.respCode=="0"){
//    		 if(data.mastlist.length>0){
//    			 var info = data.mastlist[0];
//    			 this.code = info.code;
//    			 this.code_desc =info.code_desc;
//    			 this.display_order = info.display_order==""?0:parseInt(info.display_order);
//    			 this.rmk = info.remarks;
//    			 this.dict_logo = info.dict_logo==""?this.dft_logo:"../.."+info.dict_logo;
// //    			 $("#dict_logo").error(function(){
////    				 that.dict_logo = that.err_logo;
////    			 });
//    			 console.log(data.mastlist[0]);
//    			 if(info.detaillist.length>0){
//    				 this.dictData = info.detaillist.map(function(item){
//    					 item.idx = that.idx++;
//    					 if(item.dict_logo==""){
//    						 item.dict_logo = that.dft_logo;
//    					 }else{
//    						 item.dict_logo = "../.."+item.dict_logo;
//    					 } 
//    					 return item;
//    				 });
//    			 }   			 
//    			 this.$nextTick(() => {
//    			      that.initUploader();
//    			 });  			 
//    			 
//    		 }else{
//    			this.showError("无法获取字典数据");
//    			return false;
//    		 }    		 
//    	 }else{
//    		 this.showError(data.respDesc);
//    		 return false;
//    	 }
    	
    	
    	var jsonStr = {
    		"dict_id": this.dictId
    	}; 
        var reqData = {
           "jsonStr": JSON.stringify(jsonStr)
        };
        tools.ajaxRequestAsyncEx("pub/userdict/userDictDetailList", reqData, function(data){
           if(data.respCode=="0"){ 
        	   console.log(data);
      		 if(data.mastlist.length>0){
    			 var info = data.mastlist[0];
    			 that.code = info.code;
    			 that.code_desc =info.code_desc;
    			 that.display_order = info.display_order==""?0:parseInt(info.display_order);
    			 that.rmk = info.remarks;
    			 that.dict_logo = info.dict_logo==""?that.dft_logo:"../.."+info.dict_logo;
//    			 $("#dict_logo").error(function(){
//    				 that.dict_logo = that.err_logo;
//    			 });
    			 if(info.detaillist.length>0){
    				 that.dictData = info.detaillist.map(function(item){
    					 item.idx = that.idx++;
    					 if(item.dict_logo==""){
    						 item.dict_logo = that.dft_logo;
    					 }else{
    						 item.dict_logo = "../.."+item.dict_logo;
    					 } 
    					 return item;
    				 });
    			 }   			 
    			 that.$nextTick(() => {
    			      that.initUploader();
    			 });  			    			 
    		 }else{
    			that.showError("无法获取字典数据");
    			return false;
    		 }    		         	   
        	   
           }else{
        	   that.showError(data.respDesc);
        	   return false;
           }     
        }); 
    	
    	
    },
    upLogo:function(idx){
    	this.logo_idx =idx;  // -1 即为外部logo, 其它在表中找 idx
    	$("#picker input[type='file']").click();
    },
    initUploader:function(){
  		var that = this;
  		this.uploader = WebUploader.create({
  		    swf: '../../plugin/webuploader/Uploader.swf',
  		    server: '../../pub/attach/uploadsingle',
  		    pick: {
              id: '#picker',
              label: '选择文件',
              multiple:false
  		    },
  		    resize: false,
  		    duplicate:true,
  		    accept: {
              title: 'appendix',
              extensions: that.upload.exts,
              mimeTypes: that.upload.mimeTypes //*/*
  		    },
            fileSizeLimit: that.upload.size * 1024 * 1024, // 5M
            auto:true  
  		});
  		
  		this.uploader.on( 'fileQueued', function( file ) {
//  			that.fileName = file.name;
  		});
  		
  		this.uploader.on('uploadBeforeSend', function( block, data ){  
//  			that.fileNo= tools.getuuid(32, 16);
  			var file = block.file;
  		    data.fileSize=  file.size;
  		    if(that.fileNo!=""){
	  		    data.fileNo = that.fileNo;
  		    }
  		    data.fileName = file.name;
  		    data.fileType = file.type;
  		    data.modeName = that.upload.modeName;
  		    data.folder= that.upload.folder;
  		},2);  
  		 
  		this.uploader.on( 'uploadProgress', function( file, percentage ) {
  		    that.loading = true;
  		});
  		
  		this.uploader.on( 'uploadSuccess', function( file, response ) {
  			var filePath = response.fileList[0].filePath;
  	        var fileId = response.fileList[0].fileId;
  	        var fileNo = response.fileList[0].fileNo;
  	        var fileName = response.fileList[0].fileName;
  	       	
  	        that.$message({
	            showClose: true,
	            type: 'success',
	            message: "上传成功"
	        });
  	        
  	        if(that.logo_idx==-1){
  	        	that.dict_logo = "../.."+filePath;		
  	        }else{
  	        	that.dictData.forEach(function(item,idx){
  	        		if(item.idx==that.logo_idx){
  	        			item.dict_logo = "../.."+filePath;
  	        		}
  	        	});
  	        } 	        
  	        
  	      
  		});

  		this.uploader.on( 'uploadError', function( file ) {
  			that.showError("导入失败");    			
  		});
  		
  		this.uploader.on( 'uploadComplete', function( file ) {
  			that.loading = false;
  		});
    	
    	
    },
    addRow:function(){
    	var row = {
    		"code":"",
    		"code_desc":"",
    		"display_order":0,
    		"rmk":"",
    		"dict_logo":this.dft_logo,
    		"idx": this.idx++
    	};
    	this.dictData.push(row);
    },
    delDict:function(row){
    	var that = this;
    	for(var i=0;i<this.dictData.length;i++){
    		if(that.dictData[i].idx == row.idx){
    			that.dictData.splice(i,1);
    		}
    	} 	
    },
    check:function(){
    	var that =this;
    	if(this.code.trim()==""){
    		this.showError("字典编码不能为空");
    		return false;
    	}
    	if(this.code_desc.trim()==""){
    		this.showError("字典名称不能为空");
    		return false;
    	}
    	var valid = true;
    	for(var i=0;i<this.dictData.length;i++){
    		var item = that.dictData[i];
    		if(item.code.trim()==""){
    			that.showError("字典编码不能为空");
    			valid = false;
    			break;
    		}
    		if(item.code_desc.trim()==""){
    			that.showError("字典名称不能为空");
        		valid= false;
        		break;
    		}
    	}
    	if(!valid){
    		return false;
    	}
    	
    	return true;
    },    
    save:function(){
    	var that =this;
    	if(!this.check()){
    		return false;
    	}
    	
    	var logo = this.dict_logo.replace("../..","");
    	var dictData = JSON.parse(JSON.stringify(this.dictData));
    	dictData = dictData.map(function(item){
    		item.dict_logo = item.dict_logo.replace("../..","");
    		return item;
    	});
    	
    	var jsonStr = {
    	   "dict_id": this.dictId,
    	   "code": this.code,
    	   "code_desc": this.code_desc,
    	   "dict_logo": logo,
    	   "display_order":this.display_order==''?0:this.display_order,
    	   "remarks": this.rmk,
    	   "detaillist": dictData
    	}; 
    	var reqData = {
           "jsonStr": JSON.stringify(jsonStr)
        };
        tools.ajaxRequestAsyncEx("pub/userdict/userDictDetailSave", reqData, function(data){
        	if(data.respCode=="0"){
        		that.$message({
                    showClose: true,
                    type: 'success',
                    message: "保存成功"
                }); 
        		
        		setTimeout(function(){
        			var arg={};
        			arg.msg='保存成功';
        			parent.postMessage("jwb-dialog-close-change-"+JSON.stringify(arg), "*");
        		}, 2000);
        	}else{
        		that.showError(data.respDesc);
        		return false;
        	}        	
        });
    	
    	
    	
    },
    back:function(){
    	 parent.postMessage("jwb-dialog-close", "*");
    },
    closePopup: function() {
        // console.log(this.$refs["btn"])
        this.$refs["btn"].click();
    },
   
    showError: function(info){
        var i = (info==undefined?"加载数据错误":info);
        this.$alert(i, '系统错误', {
             confirmButtonText: '确定'
        }); 
    },   


  },
  watch: {
	
  }
  
});